# !/usr/bin/env python
# coding=utf-8
# @Time    : 2017/12/21 11:08
# @Author  : 6imensiva
# @Site    : https://gitee.com/dimensiva/wx_server_prototype.git
# @File    : my_file.py
# @Software: PyCharm
# @Version: 3.5.4
import json
import sys
import os
from py3_utils import my_path
from iims import iims_config


def save_json_file(data_dict, path):
	with open(path, 'w+', encoding='utf-8') as json_file:
		json_file.write(json.dumps(data_dict, ensure_ascii=False))


def load_json_file(path_list):
	global path
	path = ''
	for i in path_list:
		path = os.path.join(path, i)
	path = resource_path(path)
	with open(path, mode='r', encoding='utf-8') as json_file:
		data = json.load(json_file)
		return data


def read_file(path_list):
	global path
	path = ''
	ret = ''
	for i in path_list:
		path = os.path.join(path, i)
	if iims_config.DevConfig.PUB:
		path = resource_path(path)
	else:
		path = my_path.PathUtil.rootPath + '/' + path
		path = path.replace('\\', '/')
	# with open(path, mode='r', encoding='utf-8') as file:
	# 	return file.read()
	file = open(path, mode='r', encoding='utf-8')
	while 1:
		line = file.readline()
		if not line:
			break
		ret += line.replace('\n', '')+'&'
	file.close()
	return ret


# 生成资源文件目录访问路径
def resource_path(relative_path):
	if getattr(sys, 'frozen', False):  # 是否Bundle Resource
		base_path = sys._MEIPASS
	else:
		base_path = os.path.abspath(".")
	return os.path.join(base_path, relative_path)


if __name__ == '__main__':
	load_json_file(['aaa', 'bbb', 'ccc'])
